package com.fr.data.util.function;

import com.fr.report.script.Primitive;
import com.fr.report.script.core.FunctionHelper;

public class AverageFunction extends AbstractDataFunction
{
  private double sumResult;
  private double countResult;

  public AverageFunction()
  {
    reset();
  }

  public void reset()
  {
    this.sumResult = 0.0D;
    this.countResult = 0.0D;
  }

  public void addData(Object paramObject)
  {
    if ((paramObject == null) || (paramObject == Primitive.NULL))
      return;
    if (paramObject instanceof Number)
      this.sumResult += ((Number)paramObject).doubleValue();
    else if (paramObject instanceof Boolean)
      if (((Boolean)paramObject).booleanValue())
        this.sumResult += 1.0D;
    else
      try
      {
        this.sumResult += Double.parseDouble(paramObject.toString());
      }
      catch (NumberFormatException localNumberFormatException)
      {
      }
    this.countResult += 1.0D;
  }

  public Object getResult()
  {
    return FunctionHelper.parsePrimitiveDouble((this.countResult == 0.0D) ? 0.0D : this.sumResult / this.countResult);
  }

  public String getFormula()
  {
    return "average";
  }
}